package com.gxa.controller;

import com.gxa.BusinessException;
import com.gxa.pojo.*;
import com.gxa.service.*;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Tian同學
 * @Date 2020/9/21 19:16
 * @Version 1.0
 */

@Controller
@RequestMapping("/purchase")
@Api(value = "执行采购操作的控制器", tags = "采购接口的API")
@CrossOrigin
public class PurchaseController {

    @Resource
    PurchaseService purchaseService;
    @Autowired
    CommodityDetailsService commodityDetailsService;

    /**
     * 分页查询
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/findAllPurchaseByPage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购管理 分页查询 接口#####", notes = "获取所有的采购数据列表", httpMethod = "post", response = String.class)
    public Map findAllPurchaseByPage(@RequestBody Map map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        String number = map.get("number").toString();
        String size = map.get("size").toString();
        System.out.println("number:" + number);
        System.out.println("size:" + size);
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit = Integer.parseInt(size);
        List<Purchase> allPurchaseByPage = purchaseService.findAllPurchaseByPage(offset, limit);
        // 采购管理-分页查询总条数
        Integer count = purchaseService.findAllPurchase();
        jsonResult.setData(allPurchaseByPage);
        jsonResult.put("count", count);
        if (allPurchaseByPage != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("系统开小差啦，请稍后再试~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购管理-查询供货厂商-下拉框
     *
     * @return
     */
    @RequestMapping(value = "/selectAllPurchase", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购管理-查询供货厂商-下拉框 接口#####", notes = "采购管理获取所有供货商", httpMethod = "post", response = String.class)
    public Map selectAllPurchase() throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        List<Purchase> purchases = purchaseService.selectAllPurchase();
        jsonResult.setData(purchases);
        if (purchases != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购管理-筛选查询 创建日期-供货厂商-采购状态 模糊查询
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/selectByLike", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购管理-筛选查询 创建日期-供货厂商-采购状态 接口#####", notes = "筛选查询", httpMethod = "post", response = String.class)
    public Map selectByLike(@RequestBody Map map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        String number = map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit = Integer.parseInt(size);
        map.put("offset", offset);
        map.put("limit", limit);
        List<Map<String, Object>> purchases = purchaseService.selectByLike(map);
        // 采购管理-筛选查询 创建日期-供货厂商-采购状态 模糊查询总条数
        Integer count = purchaseService.selectByLikeCount(map);
        jsonResult.put("count", count);
        jsonResult.setData(purchases);
        if (purchases!=null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购管理-预览商品明细 连表查询
     *
     * @param s
     * @return
     */
    @RequestMapping(value = "/findAllPurchaseByDetailed", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购管理-预览商品明细 接口#####", notes = "获取所有采购数据列表", httpMethod = "post", response = String.class)
    public Map findAllPurchaseByDetailed(@RequestParam(required = false) Map<String, Object> s) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Integer id = Integer.valueOf(s.get("id").toString());
        List<Purchase> allPurchaseByDetailed = purchaseService.findAllPurchaseByDetailed(id);
        jsonResult.setData(allPurchaseByDetailed);

        if (allPurchaseByDetailed != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购管理-修改数据-编辑
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateByIdPurchase", method = RequestMethod.POST)
    @ApiOperation(value = "#########采购管理-修改数据-编辑 接口###########", notes = "采购修改数据", httpMethod = "post", response = String.class)
    public Map updateByIdPurchase(@RequestBody Map<String, Object> map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Map purchase = (Map) map.get("purchase");
        int id = Integer.parseInt(purchase.get("id").toString());
        purchase.put("id", id);
        int i = purchaseService.updateByIdPurchase(purchase);
        commodityDetailsService.deleteByCD1(id);
        List<Map> customer = (List<Map>) map.get("customer");
        for (Map o : customer) {
            o.put("purchase_id",id);
            jsonResult.setData(commodityDetailsService.insert2(o));
        }
        jsonResult.setData(i);
        if (customer !=null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        } else {
            throw new BusinessException("修改失败~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购申请-插入新数据 添加按钮
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/insertByPurchase", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "########采购申请-插入新数据 添加接口######", notes = "采购插入数据", httpMethod = "post", response = String.class)
    public Map insertByPurchase(@RequestBody(required = false) Map<String, Object> map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Map purchase = (Map) map.get("purchase");
        // 获取当前日期
        Date date = new Date();
        // 插入当前日期到数据表中
        purchase.put("creationDate", date);

        // 设置获取时间格式
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");

        String ss= purchase.get("purchaseDate").toString();

        map.put("purchaseDate",ss);
        // 获取string类型时间
        String format = sim.format(date);
        // 去掉时间中的连接符号
        String s = format.replaceAll("[[\\s-:punct:]]", "");
        purchase.put("oddNumbers", "CG"+s);
        purchase.put("state",0);
        purchase.put("delete_id",0);
        int insert = purchaseService.insertByPurchase(purchase);
        List<Map> customer = (List<Map>) map.get("customer");
        // 采购申请-获取新添加数据的ID(最大ID)
        int i = purchaseService.selectPurchaseId();
        for (Map o : customer) {
            o.put("purchaseId", i);
            jsonResult.setData(commodityDetailsService.insert(o));
        }
        jsonResult.setData(insert);
        if (customer !=null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("提交成功");
        } else {
            throw new BusinessException("提交失败~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购申请-根据id删除
     *
     * @param s
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteByPurchase", method = RequestMethod.POST)
    @ApiOperation(value = "########采购申请-根据id删除 接口########", notes = "采购删除数据", httpMethod = "post", response = String.class)
    public Map deleteByPurchase(@RequestBody Map<String, Object> s) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Integer id = Integer.parseInt(s.get("id").toString());
        int n = purchaseService.deleteByPurchase(id);
        if (n > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        } else {
            throw new BusinessException("删除失败~~~~");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购管理-插入审批意见+状态
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/insertByApproval", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购管理-审批意见插入+状态改变 接口#####", notes = "审批", httpMethod = "post", response = String.class)
    public Map insertByApproval(@RequestBody Map map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Date date = new Date();
        map.put("approvalTime", date);
        int i = purchaseService.insertByApproval(map);
        jsonResult.setData(i);
        if (i > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("操作成功");
        } else {
            throw new BusinessException("操作失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购管理-插入审批意见+状态
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/insertByApproval2", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购管理-入库+状态改变 接口#####", notes = "入库", httpMethod = "post", response = String.class)
    public Map insertByApproval2(@RequestBody Map map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Date date = new Date();
        map.put("requiredDate", date);
        int i = purchaseService.insertByApproval(map);
        int i1 = purchaseService.insertByApproval2(map);
        jsonResult.setData(i);
        jsonResult.setData(i1);
        if (i > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("操作成功");
        } else {
            throw new BusinessException("操作失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购申请-查询供货厂商-下拉框
     *
     * @return
     */
    @RequestMapping(value = "/selectByTb_GoodsstorageSupplier", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购申请-查询供货厂商-下拉框接口######", notes = "采购申请获取所有供货商", httpMethod = "post", response = String.class)
    public Map selectByTb_GoodsstorageSupplier() throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        List<Tb_Goodsstorage> tb_goodsstorages = purchaseService.selectByTb_GoodsstorageSupplier();
        jsonResult.setData(tb_goodsstorages);
        if (tb_goodsstorages != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购申请-查询审批人-下拉框
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/selectByApproved", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购申请-查询审批人-下拉框接口######", notes = "采购申请-查询审批人-下拉框", httpMethod = "post", response = String.class)
    public Map selectByApproved() throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        List<User> users = purchaseService.selectByApproved();
        jsonResult.setData(users);
        if (users != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购申请-查询商品品牌-选择品牌下拉框
     *
     * @return
     */
    @RequestMapping(value = "/selectByTb_GoodsstorageBrand", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购申请-查询商品品牌-选择品牌下拉框接口######", notes = "采购申请-查询商品品牌", httpMethod = "post", response = String.class)
    public Map selectByTb_GoodsstorageBrand() throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        List<Tb_Goodsstorage> tb_goodsstorages = purchaseService.selectByTb_GoodsstorageBrand();
        jsonResult.setData(tb_goodsstorages);
        if (tb_goodsstorages != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 采购申请-选择商品分类-选择分类下拉框
     *
     * @return
     */
    @RequestMapping(value = "/selectByTb_GoodsstorageBrandClassification", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购申请-选择商品分类-选择分类下拉框接口######", notes = "采购申请-选择商品品牌分类", httpMethod = "post", response = String.class)
    public Map selectByTb_GoodsstorageBrandClassification() throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        List<Tb_Goodsstorage> tb_goodsstorages = purchaseService.selectByTb_GoodsstorageBrandClassification();
        jsonResult.setData(tb_goodsstorages);
        if (tb_goodsstorages != null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败");
        }
        return jsonResult.getValues();
    }


    /**
     * 采购申请 选择品牌-选择分类-商品标题 模糊查询
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/selectByTb_GoodsstorageLike", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####采购申请 选择品牌-选择分类-商品标题 模糊查询 接口#####", notes = "采购申请-选择商品 选择品牌-选择分类-商品标题查询", httpMethod = "post", response = String.class)
    public Map selectByTb_GoodsstorageLike(@RequestBody Map map) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        String number = map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit = Integer.parseInt(size);
        map.put("offset", offset);
        map.put("limit", limit);
        // 采购申请 选择品牌-选择分类-商品标题 模糊查询总条数
        Integer count = purchaseService.selectByTb_GoodsstorageLikeCount(map);
        jsonResult.put("count", count);
        List<Map<String, Object>> list = purchaseService.selectByTb_GoodsstorageLike(map);
        jsonResult.setData(list);
        if (list !=null) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        } else {
            throw new BusinessException("查询失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 删除商品明细单条数据
     *
     * @param s
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteByCD", method = RequestMethod.POST)
    @ApiOperation(value = "######删除商品明细单条数据接口########", notes = "商品明细单条删除数据", httpMethod = "post", response = String.class)
    public Map deleteByCD(@RequestBody Map<String, Object> s) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        Integer id = Integer.parseInt(s.get("id").toString());
        int n = commodityDetailsService.deleteByCD(id);
        if (n > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        } else {
            throw new BusinessException("删除失败~~~~");
        }
        return jsonResult.getValues();
    }


}
